// ----------------------- 全局函数定义 -----------------------
if (typeof outLog === 'undefined') {
  global.outLog = function (...args) { console.log(...args); }
}
if (typeof outErr === 'undefined') {
  global.outErr = function (...args) { console.error('[ERR]', ...args); }
}
if (typeof pcyLog === 'undefined') {
  global.pcyLog = function (...args) { console.log(' - [P]', ...args); }
}
if (typeof pcyErr === 'undefined') {
  global.pcyErr = function (...args) { console.error(' - [P][ERR]', ...args); }
}

if (typeof btoa === 'undefined') {
  global.btoa = function (str) { return new Buffer.from(str).toString('base64'); }
}
if (typeof atob === 'undefined') {
  global.atob = function (b64Encoded) { return new Buffer.from(b64Encoded, 'base64').toString(); };
}

if (typeof N === 'undefined') {
  global.N = Number;
}

if (typeof INT === 'undefined') {
  global.INT = parseInt;
}

if (typeof SUM === 'undefined') {
  global.SUM = function (a, b, n=2) { return Number(Number(a) + Number(b)).toFixed(n); }
}

if (typeof ABS === 'undefined') {
  global.ABS = function (a, b, n=2) { return Number(Math.abs(Number(a) - Number(b))).toFixed(n); }
}

if (typeof SUM_I === 'undefined') {
  global.SUM_I = function (a, b=0) { return parseInt(a) + parseInt(b); }
}

if (typeof ABS_I === 'undefined') {
  global.ABS_I = function (a, b=0) { return Math.abs(parseInt(a) - parseInt(b)); }
}

// 判断是否有效，可以判断入参是对象成员undefined的情况，无法判断入参就是undefined的情况
global.isVaild = function (value) {
  if (typeof value ==='undefined' || value === null) return false;
  return true;
}

// 判断是不是数字
global.isNum = function (value) {
  if (typeof value ==='undefined' || value === null) return false;
  return isFinite(value) && !isNaN(parseFloat(value));
}

// 判断是不是整数
global.isInt = function (value) {
  if (typeof value ==='undefined' || value === null) return false;
  return isFinite(value) && !isNaN(parseInt(value));
}

// 错误捕获
process.on('uncaughtException', (err)=>{
    console.log(err); // 打印出错误
    console.log(err.stack); // 打印出错误的调用栈方便调试
})